﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

using Wicresoft.Common;
using Wicresoft.JDPostBoard.Entity;
using Wicresoft.Framework.Organization;
using System.Collections.Generic;
public partial class Gift_ApplyDetail : BaseAuthenticationPage
{
    public Guid ApplyID
    {
        get
        {
            if (Request["ApplyID"] != null)
                return new Guid(Request["ApplyID"]);
            return Guid.Empty;
        }
    }


    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1 and IsEnable='True'";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }

    public bool CanEdit
    {
        get
        {
            if (ViewState["CanEdit"] == null)
                ViewState["CanEdit"] = true;
            return (bool)ViewState["CanEdit"];
        }
        set
        {
            ViewState["CanEdit"] = value;
        }
    }

    protected DataTable DTData
    {
        get
        {
            if (ViewState["DTData"] == null)
                ViewState["DTData"] = new DataTable();
            return ViewState["DTData"] as DataTable;
        }
        set
        {
            ViewState["DTData"] = value;
        }
    }

    protected DataTable DTAPPLYDATA
    {
        get
        {
            if (ViewState["ApplyTable"] == null)
            {
                ViewState["ApplyTable"] = new DataTable();
                (ViewState["ApplyTable"] as DataTable).Columns.Add("GiftID", typeof(Guid));
                (ViewState["ApplyTable"] as DataTable).Columns.Add("GiftName");
                (ViewState["ApplyTable"] as DataTable).Columns.Add("GiftPrice");
                (ViewState["ApplyTable"] as DataTable).Columns.Add("GiftUnit");
                (ViewState["ApplyTable"] as DataTable).Columns.Add("GiftCount");
                (ViewState["ApplyTable"] as DataTable).Columns.Add("GiftTotal");
                (ViewState["ApplyTable"] as DataTable).Columns.Add("Remark");
            }
            return ViewState["ApplyTable"] as DataTable;
        }
        set
        {
            ViewState["ApplyTable"] = value;
        }
    }

    public string PriceList
    {
        get
        {
            if (ViewState["PriceList"] == null)
                ViewState["PriceList"] = "";
            return ViewState["PriceList"] as string;
        }
        set
        {
            ViewState["PriceList"] = value;
        }
    }

    private string ModuleID
    {
        get
        {
            return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?><value type=""System.Guid"">{0}</value>", ConfigurationManager.AppSettings["ModuleID"]);
        }
    }
    private string SendTime
    {
        get
        {
            return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?><value type=""System.DateTime"">{0}</value>", DateTime.Now);
        }
    }
    private string SenderName
    {
        get
        {
            return CurrentUser.DisplayName;
        }
    }

    public string getReceiver(string userName, string userDisplayName, string Mobile)
    {
        return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?>
                                    <Receivers>
  			                                   <Receiver>
    				                                    <Name>{0}</Name>
    				                                    <DisplayName>{1}</DisplayName>
    				                                    <NodeType>User</NodeType>
    				                                    <Email></Email>
    				                                    <LCS></LCS>
    				                                    <Mobile>{2}</Mobile>
    				                                    <Fax></Fax>
  			                                    </Receiver>
                                    </Receivers>", userName, userDisplayName, Mobile);
    }
    public string getReceiver2(List<string> userName, List<string> userDisplayName, List<string> Mobile)
    {
        StringBuilder sb = new StringBuilder(@"<?xml version=""1.0"" encoding=""UTF-8""?><Receivers>");
        for (int i = 0; i < userName.Count; i++)
        {
            sb.AppendFormat(@"<Receiver><Name>{0}</Name><DisplayName>{1}</DisplayName>
    				                                    <NodeType>User</NodeType>
    				                                    <Email></Email>
    				                                    <LCS></LCS>
    				                                    <Mobile>{2}</Mobile>
    				                                    <Fax></Fax></Receiver>", userName[i], userDisplayName[i], Mobile[i]);
        }

        sb.Append("</Receivers>");

        return sb.ToString();
    }
    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "申请表";
        ShowAdvancedSearch = false;
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.DefaultLayout = LayoutCode.Simple.ToString();

        status.Attributes.Add("onkeydown", "countChar();");
        status.Attributes.Add("onkeyup", "countChar();");
        tbShyj.Attributes.Add("onkeydown", "countChar();");
        tbShyj.Attributes.Add("onkeyup", "countChar();");
        tbNilinqu.Attributes.Add("readonly", "readonly");
        this.tbGiftCount.Attributes.Add("onkeyup", "OnlyNum(this)");
        this.tbNilinqu.Attributes.Add("onclick", "WdatePicker({ dateFmt:'" + TimeFormat + "'})");
         
        if (!IsPostBack)
        {
            Department[] deptList = CurrentUser.GetDepartments();
            List<Department> otherList = new List<Department>();
            foreach (Department item in deptList)
            {
                if (item.DisplayName != "嘉定新城（马陆镇）" && item.DisplayName != "人社局"&& item.DisplayName!="各街镇劳动保障所")
                {
                    otherList.Add(item);
                }
            }
            if (otherList.Count > 0)
            {
                this.ltDepart.Text = otherList[0].DisplayName;
            }
            else
            {
                this.ltDepart.Text = " 嘉定新城（马陆镇）";
            }




            InitControl();


          

            this.ltApplicant.Text = CurrentUser.DisplayName.ToString();
            //this.ltDepart.Text = CurrentUser.GetMainDepartment().DisplayName;
            DateTime dtTime = DateTime.Now;
            this.ltTime.Text = dtTime.ToString(TimeFormat);
            
            tbNilinqu.Text = dtTime.ToString(TimeFormat);
            this.Order.OrderColumn = "ID";
            this.Order.OrderDirection = "ASC";
            BindData();
        }

    }

    protected string TimeFormat
    {
        get
        {

            return "yyyy-MM-dd HH:mm:ss";
        }
    }
    protected void InitControl()
    {
        this.ddlUnit.DataSource = DataAccess.Select(typeof(GiftUnit), "IsEnable='True'", "UnitName");
        this.ddlUnit.DataTextField = "UnitName";
        this.ddlUnit.DataValueField = "UnitName";
        this.ddlUnit.DataBind();
        this.ddlUnit.Items.Insert(0, new ListItem("", ""));

        DataTable dt = DataAccess.Select(typeof(Gift), "IsEnable='True'", "GiftName");
        this.ddlGiftName.DataSource = dt;
        this.ddlGiftName.DataTextField = Gift.SQLCOL_GIFTNAME;
        this.ddlGiftName.DataValueField = Gift.SQLCOL_ID;
        this.ddlGiftName.DataBind();
        this.ddlGiftName.Items.Insert(0, new ListItem("-请选择-", ""));

        foreach (DataRow dr in dt.Rows)
            PriceList += PriceList == "" ? dr["ID"] + "|" + dr["GiftPrice"] + "|" + dr["GiftUnit"] : ";" + dr["ID"] + "|" + dr["GiftPrice"] + "|" + dr["GiftUnit"];


        if (dt != null && dt.Rows.Count != 0)
            this.tbGiftPrice.Value = dt.Rows[0]["GiftPrice"].ToString();
        dt = Wicresoft.Framework.Organization.OrganizationHelper.GetUserInfoTableByDepartmentWithRole(CurrentUser.MainDepartmentID, false, new Guid(System.Configuration.ConfigurationManager.AppSettings["OfficeLeaderID"].ToLower()));


        string usedepartname = "";
        Department[] deptList = CurrentUser.GetDepartments();
        List<Department> otherList = new List<Department>();
        foreach (Department item in deptList)
        {
            if (item.DisplayName != "嘉定新城（马陆镇）" && item.DisplayName != "人社局" && item.DisplayName != "各街镇劳动保障所")
            {
                otherList.Add(item);
            }
        }
        if (otherList.Count > 0)
        {
            usedepartname = otherList[0].DisplayName;
        }
        else
        {
            usedepartname = " 嘉定新城（马陆镇）";
        }





        if (AppConfig.Leaders.IndexOf("," + CurrentUser.Name + ",", StringComparison.CurrentCultureIgnoreCase) != -1)
        {
            dt = new DataTable();
            dt.Columns.Add("DisplayName");
            dt.Columns.Add("ID");

            DataRow dr = dt.NewRow();
            dr["DisplayName"] = CurrentUser.DisplayName;
            dr["ID"] = CurrentUser.ID;
            dt.Rows.Add(dr);
            this.ddlAuditor.DataSource = dt;
            this.ddlAuditor.DataTextField = "DisplayName";
            this.ddlAuditor.DataValueField = "ID";
            this.ddlAuditor.DataBind();

        }

        else
        {
            dt = DataAccess.ExecuteSql("JDPB.ConnectionString", "select UserID as ID,UserDisplayName as DisplayName from giftcharge where  DepartmentDisplayName='" + usedepartname + "'").Tables[0];

            this.ddlAuditor.DataSource = dt;
            this.ddlAuditor.DataTextField = "DisplayName";
            this.ddlAuditor.DataValueField = "ID";
            this.ddlAuditor.DataBind();

        }
      

        if (ApplyID != Guid.Empty)
        {
            ListItem itemAll = new ListItem();

            ApplyLog applytemp = new ApplyLog(ApplyID);
            applytemp.Load();
            itemAll.Text = applytemp.AuditorDisplay;
         
            itemAll.Value = applytemp.AuditorID;
         
            this.ddlAuditor.Items.Insert(0, itemAll);
          
        }
        else
        {
         
        }

    }


    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        if (ApplyID != Guid.Empty)
        {
            this.tdFg1.Visible = false;
            this.tdClose.Visible = false;

            ApplyLog apply = new ApplyLog(ApplyID);
            apply.Load();
            this.ltApplicant.Text = apply.ApplierDsName;
            this.ltDepart.Text = apply.ApplierDepartment;
            this.ltTime.Text = apply.ApplyTime.ToString();
            this.tbNilinqu.Text = apply.NiLingquTime.ToString();
            this.status.Value = apply.Remark;
            this.tbTitle.Text = apply.ApplyTitle;
            this.tbApplyCost.Value = apply.ApplyCost.ToString("N");
            
            ListItem li = ddlAuditor.Items.FindByValue(apply.AuditorID);
            if (li != null)
            {
                this.ddlAuditor.SelectedValue = li.Value;
            }

            if (apply.Status == Enum.ApplyStatusEnum.待审核.ToString())
            {
                this.tdFg2.Visible = true;
                this.tdCancel.Visible = true;
                this.btnSupply.Visible = false;
                this.btnSave.Visible = true;
                
            }
            else if (apply.Status == Enum.ApplyStatusEnum.审核通过.ToString())
            {
                this.tdApply.Visible = false;
                this.tdCancel.Visible = true;
                this.tdArrival.Visible = true;
                this.trShyj.Visible = true;
                this.tbShyj.Value = apply.DealContent;

                
               

            }
            else if (apply.Status == Enum.ApplyStatusEnum.延后审核.ToString())
            {
                this.tdApply.Visible = false;
                this.tdCancel.Visible = true;
                this.tdArrival.Visible = true;
                this.trShyj.Visible = true;
                this.tbShyj.Value = apply.DealContent;
            }
            else if (apply.Status == Enum.ApplyStatusEnum.已领取.ToString())
            {
                this.tdApply.Visible = false;
                this.tdCancel.Visible = false;
                this.tdArrival.Visible = false;
                this.trShyj.Visible = false;
                this.tbShyj.Value = apply.DealContent;
            }


            DTAPPLYDATA = DataAccess.Select(typeof(Apply_Gift_Relation), "ApplyID='" + ApplyID + "'");
            this.rptProduct.DataSource = DTAPPLYDATA;
            this.rptProduct.DataBind();

            li = ddlStatus.Items.FindByValue(apply.Status);

            if (apply.Status=="审核通过")
            {
                ListItem item1 = new ListItem();
                item1 = ddlStatus.Items.FindByValue("即时审核");
                //item1.Value = "即时审核";
                //item1.Text = "即时审核";

                ddlStatus.SelectedValue = item1.Value; ;
                 ddlStatus.Enabled = false;
            }
            if (li != null)
            {
                ddlStatus.SelectedValue = li.Value;
                ddlStatus.Enabled = false;
            }
        }
            
        else
        {
            DataTable dt = Wicresoft.Framework.Organization.OrganizationHelper.GetUserInfoTableByDepartmentWithRole(CurrentUser.MainDepartmentID, false, new Guid(System.Configuration.ConfigurationManager.AppSettings["OfficeLeaderID"].ToLower()));
            if (dt.Rows.Count >= 1)
            {
                ListItem li = ddlAuditor.Items.FindByValue(dt.Rows[0]["ID"].ToString());
                if (li != null)
                {
                    this.ddlAuditor.SelectedValue = li.Value;
                }

            }
        }


    }

    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Delete":
                if (!CanEdit)
                {
                    WebCommon.ShowMessage(this, "提交的申请状态已改变，不能删除");
                    break;
                }
                DTAPPLYDATA.Rows.RemoveAt(e.Item.ItemIndex);
                rptProduct.DataSource = DTAPPLYDATA;
                rptProduct.DataBind();
                GetPriceTotal();
                break;

        }
    }

    protected void btnSupply_Click(object sender, EventArgs e)
    {
        if (DTAPPLYDATA.Rows.Count == 0)
        {
            string script = "<script>alert('礼品列表为空')</script>";
            ClientScript.RegisterStartupScript(this.GetType(), "Close", script);
        }
        else
        {

            if (this.status.InnerText.Length > 200)
            {
                Response.Write("<script>alert('备注过长，请删减');window.location.href=window.location.href;</script>");
                rptProduct.DataSource = DTAPPLYDATA;
                rptProduct.DataBind();
            }
            else
            {
                ApplyLog order = new ApplyLog();
                order.ApplyID = ApplyID;
                order.Load();
                if (ApplyID == Guid.Empty)
                {
                    order.ApplyID = Guid.NewGuid();
                    order.ApplierDepartment = this.ltDepart.Text;

                    order.ApplierID = CurrentUser.ID.ToString();
                    order.ApplierName = CurrentUser.Name;
                    order.ApplierDsName = this.ltApplicant.Text;
                    
                    order.ApplyTime = Convert.ToDateTime(this.ltTime.Text);
                    order.NiLingquTime = Convert.ToDateTime(this.ltTime.Text);
                }
                order.ApplyTitle = this.tbTitle.Text;
                if (tbNilinqu.Text.Trim() != string.Empty)
                {

                    order.NiLingquTime = Convert.ToDateTime(this.tbNilinqu.Text);
                }
                if (ddlStatus.SelectedValue == Enum.ApplyStatusEnum.延后审核.ToString())
                {
                    order.Status = Enum.ApplyStatusEnum.延后审核.ToString();
                }

                else
                    order.Status = Enum.ApplyStatusEnum.待审核.ToString();

                order.IsEnable = true;
                order.Remark = this.status.Value;

                double Sum = 0;
                string giftDetails = string.Empty;
                DataAccess.Delete(typeof(Apply_Gift_Relation), "ApplyID='" + ApplyID + "'");
                foreach (DataRow dr in DTAPPLYDATA.Rows)
                {
                    Apply_Gift_Relation agr = new Apply_Gift_Relation();
                    agr.ApplyID = order.ApplyID;
                    agr.GiftID = new Guid(dr["GiftID"].ToString());
                    agr.GiftName = dr["GiftName"].ToString();
                    agr.GiftPrice = double.Parse(dr["GiftPrice"].ToString());
                    agr.GiftCount = double.Parse(dr["GiftCount"].ToString());
                    agr.GiftTotal = double.Parse(dr["GiftTotal"].ToString());
                    agr.GiftUnit = dr["GiftUnit"].ToString();
                    agr.Remark = dr["Remark"].ToString();
                    Sum += agr.GiftTotal;
                    agr.Save();
                    giftDetails += "," + dr["GiftName"].ToString() + dr["GiftCount"].ToString() + dr["GiftUnit"].ToString()+"份";
                }
                giftDetails = giftDetails.Trim(new char[] { ',' });
                order.ApplyCost = Sum;

                order.AuditorID = this.ddlAuditor.SelectedValue;
                //order.AuditorName = this.tbAuditorName.Value;
                order.AuditorDisplay = this.ddlAuditor.SelectedItem.Text;

                order.Save();

                if (Request["ApplyID"] == null)
                {
                    #region 发送申请提醒
                    User u = OrganizationPublicHelper.GetUserInfo(new Guid(this.ddlAuditor.SelectedValue));
                    //MsgUtil msgUtil = new MsgUtil(u.ID.ToString(), u.Name, u.DisplayName, string.Empty);

                    
                    
                    string msg = "{0}因{1}于{2}申请礼品:";

                    msg = string.Format(msg, CurrentUser.DisplayName, this.tbTitle.Text, DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"), tbApplyCost.Value);
                    //msgUtil.SendMessage(CurrentUser.Name, string.Empty, "礼品申请：" + this.tbTitle.Text, msg, msg);


                    MessageService.MessagesService service = new MessageService.MessagesService();
                    DataTable dtData = Wicresoft.Common.DataAccess.ExecuteSql("Wicresoft.Framework.ConnectionString",
                        string.Format(@"Select ValueString from PropertyData where NodeID='{0}' and PropertyID in (
                    select ID from Property where Name='Mobile')", u.ID.ToString())).Tables[0];
                    string mobile = dtData.Rows.Count == 0 ? "" : dtData.Rows[0]["ValueString"].ToString();

                    List<string> userList = new List<string>();
                    List<string> userNameList = new List<string>();
                    List<string> userMobileList = new List<string>();
                    if (!string.IsNullOrEmpty(mobile))
                    {
                        userList.Add(u.Name);
                        userNameList.Add(u.DisplayName);
                        userMobileList.Add(mobile);

                    }

                    #region 延后审核发短信给审核人和发货人

                    if (order.Status == Enum.ApplyStatusEnum.延后审核.ToString())
                    {
                        User[] users = OrganizationHelper.GetUserInfoListByRole(AppConfig.DistributorRoleID);
                        foreach (User item in users)
                        {
                            DataTable dtData2 = Wicresoft.Common.DataAccess.ExecuteSql("Wicresoft.Framework.ConnectionString",
                         string.Format(@"Select ValueString from PropertyData where NodeID='{0}' and PropertyID in (
                    select ID from Property where Name='Mobile')", u.ID.ToString())).Tables[0];
                            string mobile2 = dtData2.Rows.Count == 0 ? "" : dtData2.Rows[0]["ValueString"].ToString();

                            if (mobile2 != string.Empty)
                            {
                                if (!userList.Contains(item.Name))
                                {
                                    userList.Add(item.Name);
                                    userNameList.Add(item.DisplayName);
                                    userMobileList.Add(mobile2);
                                }
                            }
                        }

                    }

                    #endregion

                    string status = service.SendMessageByModuleID(ModuleID, SenderName, getReceiver2(userList, userNameList, userMobileList), msg + giftDetails, "", false, 1, string.Empty, SendTime);

                    #endregion

                    string script = "<script>alert('申领已提交审核!');refreshParent();window.close();</script>";
                    ClientScript.RegisterStartupScript(this.GetType(), "Close", script);
                }
                else
                {
                    string script = "<script>alert('保存成功!');parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>";
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh2", script);
                }

            }


        }

    }
    protected void btnDeleteMul_Click(object sender, EventArgs e)
    {

    }
    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        if (this.ddlGiftName.SelectedValue == "")
        {
            string script = "<script>alert('请先选择礼品')</script>";
            ClientScript.RegisterStartupScript(this.GetType(), "Selected", script);
            this.ddlGiftName.Focus();
            return;
        }

        if (string.IsNullOrEmpty(tbGiftCount.Text))
        {
            ClientScript.RegisterStartupScript(GetType(), "EmptyProductCount", "<script>alert('请填写申请礼品数量');</script>");
            return;
        }
        if (DTAPPLYDATA.Select("GiftName='" + this.ddlGiftName.SelectedItem.Text + "'").Length > 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "IsExist", "<script>alert('礼品已经存在');</script>");
            return;
        }
        DataRow dr = DTAPPLYDATA.NewRow();
        dr["GiftID"] = Guid.NewGuid();
        dr["GiftName"] = this.ddlGiftName.SelectedItem.Text.TrimStart();
        dr["GiftPrice"] = this.tbGiftPrice.Value;
        dr["GiftUnit"] = this.ddlUnit.SelectedItem.Text;
        dr["GiftCount"] = this.tbGiftCount.Text;
        dr["GiftTotal"] = float.Parse(this.tbGiftPrice.Value) * float.Parse(this.tbGiftCount.Text);
        dr["Remark"] = txtGiftRemark.Text.Trim();
        DTAPPLYDATA.Rows.Add(dr);

        rptProduct.DataSource = DTAPPLYDATA;
        rptProduct.DataBind();
        tbGiftCount.Text = "";
        txtGiftRemark.Text = "";

        GetPriceTotal();
        this.ddlGiftName.SelectedIndex = 0;
        this.tbGiftPrice.Value = string.Empty;
    }

    protected void lbtnCancel_Click(object sender, EventArgs e)
    {
        if (ApplyID == Guid.Empty)
            return;
        ApplyLog order = new ApplyLog();
        order.ApplyID = ApplyID;
        order.Load();
        order.Status = Enum.ApplyStatusEnum.已取消.ToString();
        order.Save();
        Page.RegisterStartupScript("refresh", "<script>alert('取消成功!');parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>");
    }

    protected void Arrival_Click(object sender, EventArgs e)
    {
        ApplyLog ol = new ApplyLog(ApplyID);
        if (ol.Load())
        {

            ol.ApplyTitle = this.tbTitle.Text;
            ol.DealContent = tbShyj.Value;

            ol.Remark = this.status.Value;

        }
        if (ol.Status == Enum.ApplyStatusEnum.审核通过.ToString())
        {

            double Sum = 0;
            DataAccess.Delete(typeof(Apply_Gift_Relation), "ApplyID='" + ApplyID + "'");
            foreach (DataRow dr in DTAPPLYDATA.Rows)
            {
                Apply_Gift_Relation agr = new Apply_Gift_Relation();
                agr.ApplyID = ol.ApplyID;
                agr.GiftID = new Guid(dr["GiftID"].ToString());
                agr.GiftName = dr["GiftName"].ToString();
                agr.GiftPrice = double.Parse(dr["GiftPrice"].ToString());
                agr.GiftCount = double.Parse(dr["GiftCount"].ToString());
                agr.GiftTotal = double.Parse(dr["GiftTotal"].ToString());
                agr.GiftUnit = dr["GiftUnit"].ToString();
                agr.Remark = dr["Remark"].ToString();
                Sum += agr.GiftTotal;
                agr.Save();
            }
            ol.ApplyCost = Sum;

            //ol.AuditorID = this.ddlAuditor.SelectedValue;
            ////order.AuditorName = this.tbAuditorName.Value;
            //ol.AuditorDisplay = this.ddlAuditor.SelectedItem.Text;


            ol.Status = Enum.ApplyStatusEnum.已领取.ToString();
            ol.ArrivalTime = DateTime.Now;
            ol.ArrivalOperatorID = CurrentUser.ID.ToString();
            ol.ArrivalOperatorName = CurrentUser.Name;
            ol.ArrivalOperatorDsName = CurrentUser.DisplayName;
            ol.Save();
            //if (ol.Save())
            //{
            //    string MessageTitle = "到货通知";
            //    string MessageText = "您的申请“" + ol.ApplyTitle + "”的已经到货，请及时领取！";
            //    string MessageModuleID = WebCommon.GetConfig("MessageModuleID");
            //    MessagesService ms = new MessagesService();
            //    string s = ms.SendMessageByModuleID(MessageModuleID, CurrentUser.Name, ol.ApplierName, MessageTitle, MessageText, false, 0, "", DateTime.Now.ToString());
            //    //WebCommon.ShowMessage(this, "通知成功");
            //}

        }

        else if (ol.Status == Enum.ApplyStatusEnum.延后审核.ToString())
        {

            double Sum = 0;
            DataAccess.Delete(typeof(Apply_Gift_Relation), "ApplyID='" + ApplyID + "'");
            foreach (DataRow dr in DTAPPLYDATA.Rows)
            {
                Apply_Gift_Relation agr = new Apply_Gift_Relation();
                agr.ApplyID = ol.ApplyID;
                agr.GiftID = new Guid(dr["GiftID"].ToString());
                agr.GiftName = dr["GiftName"].ToString();
                agr.GiftPrice = double.Parse(dr["GiftPrice"].ToString());
                agr.GiftCount = double.Parse(dr["GiftCount"].ToString());
                agr.GiftTotal = double.Parse(dr["GiftTotal"].ToString());
                agr.GiftUnit = dr["GiftUnit"].ToString();
                agr.Remark = dr["Remark"].ToString();
                Sum += agr.GiftTotal;
                agr.Save();
            }
            ol.ApplyCost = Sum;

            //ol.AuditorID = this.ddlAuditor.SelectedValue;
            ////order.AuditorName = this.tbAuditorName.Value;
            //ol.AuditorDisplay = this.ddlAuditor.SelectedItem.Text;


            ol.Status = Enum.ApplyStatusEnum.延后审核已发货.ToString();
            ol.ArrivalTime = DateTime.Now;
            ol.ArrivalOperatorID = CurrentUser.ID.ToString();
            ol.ArrivalOperatorName = CurrentUser.Name;
            ol.ArrivalOperatorDsName = CurrentUser.DisplayName;
            ol.Save();
            //if (ol.Save())
            //{
            //    string MessageTitle = "到货通知";
            //    string MessageText = "您的申请“" + ol.ApplyTitle + "”的已经到货，请及时领取！";
            //    string MessageModuleID = WebCommon.GetConfig("MessageModuleID");
            //    MessagesService ms = new MessagesService();
            //    string s = ms.SendMessageByModuleID(MessageModuleID, CurrentUser.Name, ol.ApplierName, MessageTitle, MessageText, false, 0, "", DateTime.Now.ToString());
            //    //WebCommon.ShowMessage(this, "通知成功");
            //}

        }

        BindData();
        ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script>parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>");


    }

    protected void GetPriceTotal()
    {
        if (DTAPPLYDATA == null || DTAPPLYDATA.Rows.Count == 0)
            this.tbApplyCost.Value = "";
        else
        {
            float temp = 0;
            foreach (DataRow dr in DTAPPLYDATA.Rows)
                temp += float.Parse(dr["GiftTotal"].ToString());

            this.tbApplyCost.Value = temp.ToString("N");
        }
    }

}
